package bl.controller;

import java.util.ArrayList;
import java.util.Iterator;
import po.BookInventoryPO;
import po.BookPO;
import database.factory.BookDataFactory;
import database.factory.BookDataFactoryImpl;
import database.factory.BookInventoryDataFactory;
import database.factory.BookInventoryDataFactoryImpl;
import database.service.BookDataService;
import database.service.BookInventoryDataService;
import vo.BLToViewResultMessage;
import vo.BookVO;
import bl.domain.BookManagement;
import bl.service.BookManagementBLService;

public class BookManagementController implements BookManagementBLService {

	private BookManagement bookManagement;

	public BookManagementController() {
		bookManagement = new BookManagement();
	}

	private void init() {
		BookDataFactory bookDataFactory = new BookDataFactoryImpl();
		BookDataService bookDataService = bookDataFactory.getBookDataService();
		bookDataService.load();
		bookManagement.setBookItr(bookDataService.getIterator());
	}

	@Override
	public ArrayList<BookVO> getBookList() {
		init();
		ArrayList<BookVO> bookVOs = new ArrayList<BookVO>();
		Iterator<BookPO> bookItr = bookManagement.getBookItr();
		while (bookItr.hasNext()) {
			BookPO bpo = bookItr.next();
			if (bpo.getId() > 0) {
				BookVO bvo = new BookVO(bpo);
				bookVOs.add(bvo);
			}
		}
		return bookVOs;
	}

	@Override
	public BLToViewResultMessage addBook(BookVO book, int quantity) {
		if (book == null) {
			return null;
		}

		init();
		BookPO newBook = book.getBookPO();
		BookDataFactory bookDataFactory = new BookDataFactoryImpl();
		BookDataService bookDataService = bookDataFactory.getBookDataService();
		bookDataService.load();
		int newBookID = bookDataService.insert(newBook);
		bookDataService.save();

		if (newBookID <= 0) {
			return null;
		}

		BookInventoryDataFactory bookInventoryDataFactory = new BookInventoryDataFactoryImpl();
		BookInventoryDataService bookInventoryDataService = bookInventoryDataFactory
				.getBookInventoryDataService();
		bookInventoryDataService.load();
		BookInventoryPO bookInventory = new BookInventoryPO(newBookID, quantity);
		bookInventoryDataService.insert(bookInventory);
		bookInventoryDataService.save();
		
		return BLToViewResultMessage.ADD_BOOK_SUCCESS;
	}

	@Override
	public BLToViewResultMessage deleteBook(int bookID) {
		if (bookID == 0) {
			return null;
		}

		init();
		BookDataFactory bookDataFactory = new BookDataFactoryImpl();
		BookDataService bookDataService = bookDataFactory.getBookDataService();
		bookDataService.load();
		BookPO bookPO = bookDataService.find(bookID);
		if (bookPO == null) {
			return null;
		}
		bookDataService.delete(bookPO);
		bookDataService.save();
		
		BookInventoryDataFactory bookInventoryDataFactory = new BookInventoryDataFactoryImpl();
		BookInventoryDataService bookInventoryDataService = bookInventoryDataFactory
				.getBookInventoryDataService();
		bookInventoryDataService.load();
		BookInventoryPO bookInventory = bookInventoryDataService.find(bookID);
		if (bookInventory == null) {
			return null;
		}
		bookInventoryDataService.delete(bookInventory);
		bookInventoryDataService.save();

		return BLToViewResultMessage.DELETE_BOOK_SUCCESS;
	}

	@Override
	public BLToViewResultMessage modifyBook(int bookID, BookVO book) {
		if (bookID == 0) {
			return null;
		}

		init();
		BookPO newBook = book.getBookPO();
		BookDataFactory bookDataFactory = new BookDataFactoryImpl();
		BookDataService bookDataService = bookDataFactory.getBookDataService();
		bookDataService.load();
		BookPO bookPO = bookDataService.find(bookID);
		if (bookPO == null) {
			return null;
		}
		newBook.setId(bookID);
		bookDataService.update(newBook);
		bookDataService.save();

		return BLToViewResultMessage.MODIFY_BOOK_SUCCESS;
	}

}
